home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / corewars / inst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-14  |  5.8 KB  |  345 lines

  1. /* execution code for the machine */
  2.  
  3. /*    This is a replacement for the screwed up code in "execute.c"
  4.     Each instruction is a function, and accepts just enough information
  5.     to execute that instruction.
  6. */
  7.  
  8. #include "interp.h"
  9. #include <malloc.h>
  10. #include <stdio.h>
  11.  
  12. extern    cell    a[];        /* core */
  13.  
  14. correct(thing)
  15. int    *thing;
  16. {
  17.     if (*thing >= 0)
  18.         *thing %= SIZE;
  19.     else if (*thing < 0)
  20.     {
  21.         *thing %= SIZE;
  22.         *thing = SIZE + *thing;
  23.     }
  24. }
  25.  
  26. int    getdirect(ins, off)
  27. /* get position of direct parameter */
  28. int    ins;    /* current position */
  29. int    off;    /* offset of instruction */
  30. {
  31.     int    temp;
  32.  
  33.     temp = ins + off;
  34.     correct(&temp);
  35.     return (temp);
  36. }
  37.  
  38. int    getindirect(ins, off)
  39. /* get position of indirect parameter */
  40. /* Note :-
  41. A.K. Dewdney's specifications say that the pointer points relative to its
  42. current position, NOT the instruction's position. */
  43.  
  44. int    ins;
  45. int    off;
  46. {
  47.     int    temp;    /* temporary variable */
  48.  
  49.     correct(&off);
  50.     temp = getdirect(ins, off);        /* get direct variable */
  51.     correct(&temp);                /* correct it */
  52.     temp = getdirect(temp, a[temp].para2);    /* This time it's for real */
  53.     correct(&temp);
  54.     return (temp);
  55. }
  56.  
  57. int    getpara(ins, m, p)
  58. /* Note:- This routine is designed only to work with indirect and direct
  59.    operations.  NOT with immediate mode operations. Returns the address
  60.    of the parameter by calling getdirect or getindirect            */
  61. int    ins;    /* location of current instruction */
  62. mode    m;    /* mode */
  63. int    p;    /* parameter */
  64. {
  65.     if (m == direct)
  66.         return(getdirect(ins, p));
  67.     else if (m == indirect)
  68.         return(getindirect(ins, p));
  69.  
  70.     /* else */
  71.     printf("getpara passed wrong parameter --- getpara\n");
  72.     exit(1);
  73. }
  74.  
  75. Mov    (ins, pc, pid)
  76. /* MOV instruction :
  77. sets the lastmod flag to the process id                    */
  78. int    ins;    /* position of instruction to execute */
  79. int    *pc;    /* program counter */
  80. int    pid;    /* process ID */
  81. {
  82.     int    realpos;
  83.     cell    temp;        /* store item to be moved */
  84.  
  85.     if (a[ins].m1 == immed)
  86.     {
  87.         temp.inst = dat;
  88.         temp.para1 = 0;
  89.         temp.para2 = a[ins].para1;
  90.         temp.m1 = temp.m2 = immed;
  91.     }
  92.     else
  93.     {
  94.         realpos = getpara(ins, a[ins].m1, a[ins].para1);
  95.         temp = a[realpos];
  96.     }
  97.  
  98.     if (a[ins].m2 == immed)
  99.     {
  100.         printf("Tried to mov to immediate parameter\n");
  101.         printf("--- mov\n");
  102.         exit(1);
  103.     }
  104.     else
  105.         realpos = getpara(ins, a[ins].m2, a[ins].para2);
  106.  
  107.     a[realpos] = temp;
  108.     a[realpos].lastmod = pid;
  109.     (*pc)++;
  110.     correct(pc);
  111. }
  112.  
  113. Add(ins, pc, pid)
  114. /* ADD instruction */
  115. int    ins;
  116. int    *pc;
  117. int    pid;
  118. {
  119.     int    x;        /* parameter A */
  120.     int    realpos;    /* real position of B */
  121.  
  122.     if (a[ins].m1 == immed)
  123.         x = a[ins].para1;
  124.     else
  125.         x = a[getpara(ins, a[ins].m1, a[ins].para1)].para2;
  126.  
  127.     if (a[ins].m2 == immed)
  128.     {
  129.         printf("Trying to add to immediate address\n");
  130.         printf("--- ADD\n");
  131.         exit(1);
  132.     }
  133.     else
  134.         realpos = getpara(ins, a[ins].m2, a[ins].para2);
  135.  
  136.     a[realpos].para2 += x;
  137.     a[realpos].lastmod = pid;
  138.     correct(&(a[realpos].para2));
  139.     (*pc)++;
  140.     correct(pc);
  141. }
  142.  
  143.  
  144. Sub(ins, pc, pid)
  145. /* SUB instruction */
  146. int    ins;
  147. int    *pc;
  148. {
  149.     int    x;        /* parameter A */
  150.     int    realpos;    /* real position of B */
  151.  
  152.     if (a[ins].m1 == immed)
  153.         x = a[ins].para1;
  154.     else
  155.         x = a[getpara(ins, a[ins].m1, a[ins].para1)].para2;
  156.  
  157.     if (a[ins].m2 == immed)
  158.     {
  159.         printf("Trying to subtract from immediate address\n");
  160.         printf("--- SUB\n");
  161.         exit(1);
  162.     }
  163.     else
  164.         realpos = getpara(ins, a[ins].m2, a[ins].para2);
  165.  
  166.     a[realpos].para2 -= x;
  167.     a[realpos].lastmod = pid;
  168.     correct(&(a[realpos].para2));
  169.     (*pc)++;
  170.     correct(pc);
  171. }
  172.  
  173. Jmp(ins,pc)
  174. int    ins;
  175. int    *pc;
  176. {
  177.     if (a[ins].m2 == immed)
  178.     {
  179.         printf("attempt to jump to immediate address\n");
  180.         printf("-- JMP\n");
  181.         exit(1);
  182.     }
  183.     else
  184.         *pc = getpara(ins, a[ins].m2, a[ins].para2);
  185.  
  186.     correct(pc);
  187. }
  188.  
  189. Jmz(ins, pc)
  190. int    ins;
  191. int    *pc;
  192. {
  193.     int    value;    /* value of first parameter */
  194.  
  195.     if (a[ins].m2 == immed)
  196.         value = a[ins].para2;
  197.     else
  198.     {
  199.         value = getpara(ins, a[ins].m2, a[ins].para2);
  200.         correct(&value);
  201.         value = a[value].para2;
  202.     }
  203.  
  204.     correct(&value);
  205.  
  206.     if (value)
  207.         (*pc)++;
  208.     else
  209.         *pc = getpara(ins, a[ins].m1, a[ins].para1);
  210.  
  211.     correct(pc);
  212. }
  213.  
  214. Jmn(ins,pc)
  215. int    ins;
  216. int    *pc;
  217. {
  218.     int    value;
  219.  
  220.     if (a[ins].m2 == immed)
  221.         value = a[ins].para2;
  222.     else
  223.     {
  224.         value = getpara(ins, a[ins].m2, a[ins].para2);
  225.         correct(&value);
  226.         value = a[value].para2;
  227.     }
  228.  
  229.     if (!value)
  230.         *pc = getpara(ins, a[ins].m1, a[ins].para1);
  231.     else
  232.         (*pc)++;
  233.     correct(pc);
  234. }
  235.  
  236. Djn(ins, pc, pid)
  237. int    ins;
  238. int    *pc;
  239. {
  240.     int    temp;    /* position to decrement */
  241.  
  242.     if (a[ins].m2 == immed)
  243.     {
  244.         printf("tried to decrement immediate address\n");
  245.         printf("--- DJZ\n");
  246.         exit(1);
  247.     }
  248.     else
  249.         temp = getpara(ins, a[ins].m2, a[ins].para2);
  250.  
  251.     (a[temp].para2)--;
  252.     correct(&(a[temp].para2));
  253.     a[temp].lastmod = pid;
  254.  
  255.     if (!a[temp].para2)
  256.         (*pc)++;
  257.     else
  258.         *pc = getpara(ins, a[ins].m1, a[ins].para1);
  259.  
  260.     correct(pc);
  261. }
  262.  
  263. Cmp(ins, pc)
  264. int    ins;
  265. int    *pc;
  266. {
  267.     int    value1, value2;
  268.  
  269.     if (a[ins].m1 == immed)
  270.         value1 = a[ins].para1;
  271.     else
  272.         value1 = a[getpara(ins, a[ins].m1, a[ins].para1)].para2;
  273.  
  274.     if (a[ins].m2 == immed)
  275.         value2 = a[ins].para2;
  276.     else
  277.         value2 = a[getpara(ins, a[ins].m2, a[ins].para2)].para2;
  278.  
  279.     correct(&value1);
  280.     correct(&value2);
  281.     if (value1 == value2)
  282.         (*pc) += 2;    
  283.     else
  284.         (*pc)++;
  285.  
  286.     correct(pc);
  287. }
  288.  
  289. Spl(ins, pc, mem)
  290. int    ins;
  291. int    *pc;
  292. stream    *mem;        /* pointer to structure of current pc */
  293. {
  294.     int    newpc;
  295.     stream    *newmem;
  296.  
  297.     if (a[ins].m2 == immed)
  298.     {
  299.         printf("Tried to split into immediate address\n");
  300.         printf("--- SPL\n");
  301.         exit(1);
  302.     }
  303.     else
  304.         newpc = getpara(ins, a[ins].m2, a[ins].para2);
  305.  
  306.     if (!(newmem = (stream *) malloc(sizeof(stream))))
  307.     {
  308.         printf("no more memory!!\n");
  309.         printf("--- SPL\n");
  310.         exit(1);
  311.     }
  312.  
  313.     correct(&newpc);
  314.     newmem -> pc = newpc;
  315.     newmem -> next = mem -> next;
  316.     mem -> next = newmem;
  317.     newmem -> prev = mem;
  318.     newmem -> next -> prev = newmem;
  319.  
  320.     (*pc)++;
  321.     correct(pc);
  322. }
  323.  
  324. extern    stream    *exe[];
  325.  
  326. Dat(i)
  327. /* kill stream */
  328. int    i;
  329. {
  330.     stream    *curr = exe[i];        /* current */
  331.  
  332.     if (curr -> next == curr)
  333.     {
  334.         exe[i] = NULL;
  335.         free(curr);
  336.         return;
  337.     }
  338.  
  339.     exe[i] = curr -> next;
  340.     curr -> next -> prev = curr -> prev;
  341.     curr -> prev -> next = curr -> next;
  342.  
  343.     free(curr);
  344. }
  345.